<template lang="pug">
    view()
        CuCustom(bgColor="bg-gradual-pink")
                block(slot="content") 好友邀请您一同编辑待办
        view(class="text-center text-lg" v-if="!loading")
            text(class="text-red")
                template(v-if="!task_id") 您访问的待办不存在
                template(v-if="!get_share_link_res.success") {{get_share_link_res.msg}}
                template(v-if="!cur_share_link") 待办不存在
                template(v-if="cur_share_link && cur_share_link.is_expired") 邀请已过期
        view(class="cu-bar bg-white radius margin shadow-warp" v-if="cur_share_link && !cur_share_link.is_expired")
            view(class="flex-sub text-center padding")
                text(class="text-black text-lg") 邀请您一同编辑待办: {{cur_share_link.task.task_name}}
                view(class="action margin")
                    button(class="cu-btn shadow bg-green" @click="accept_then_enter") 接受并进入
</template>
<script>
import BaseMixin from "@/mixins/BaseMixin";

export default {
  name: "task-share-accept",

  mixins: [BaseMixin],

  data() {
    return {
      task_id: null,

      loading: true,

      get_share_link_res: {},

      posting: false
    };
  },

  computed: {
    cur_share_link() {
      const res = this.get_share_link_res;
      if (!res || !res.data || !res.data.length) return null;

      return res.data[0];
    }
  },

  methods: {
    get_share_link() {
      this.get("/share_link/", { task_id: this.task_id })
        .then(res => (this.get_share_link_res = res))
        .catch(() => {})
        .then(() => (this.loading = false));
    },

    accept_then_enter() {
      const data = { task_id: this.cur_share_link.task.id };
      this.posting = true;
      this.post("/share_list/", data)
        .then(res => {
          if (!res.success) {
            //todo 显示吐司提醒
            return;
          }
          const options = {
            url: `/pages/task-items/index?id=${this.cur_share_link.task.id}`
          };
          uni.redirectTo(options);
        })
        .catch(() => {})
        .then(() => (this.posting = false));
    }
  },

  onShow() {},

  onLoad(options) {
    this.task_id = options.task_id;

    // 测试
    this.task_id = 3;

    this.task_id || (this.loading = false);

    this.get_share_link();
  }
};
</script>
<style scoped>
</style>